/*
 *  SPAGL Copyright (C) 2008 Sam Pottinger
 *  This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser 
 *  General Public License as published by the Free Software Foundation; either version 2.1 of the License, or 
 *  (at your option) any later version. This library is distributed in the hope that it will be useful, but 
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 *  See the GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License along with this library; if not, 
 *  write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 */

package spagswing;

import spagcore.SpaglCoreContainer;
import spagcore.SpaglCoreEvent;
import spagcore.SpaglCoreFont;
import spagcore.SpaglCoreImage;
import spagswing.SpaglContainer;
import spagswing.SpaglFont;
import javax.swing.JButton;

/**
 * A JButton (javax.swing.JButton) that conforms to the SpaglCoreButton interface
 * @author Sam Pottinger
 *
 */
public class SpaglButton extends JButton implements spagcore.SpaglCoreButton, spagcore.SpaglCoreEventGenerator {

	private static final long serialVersionUID = 1L;
	private SpaglCoreContainer parentWidget;
	private SpaglCoreFont widgetFont;
	private SpaglCoreImage widgetImage;
	
	/**
	 * Creates a bare bones button
	 * 
	 * @param newParent The parent container
	 */
	public SpaglButton(SpaglCoreContainer newParent)
	{
		super();
		parentWidget = newParent;
	}
	
	/**
	 * Creates a button with specified text and parent
	 * 
	 * @param newParent The parent container
	 * @param newText The text to be displayed on the face of the widget
	 */
	public SpaglButton(SpaglCoreContainer newParent, String newText)
	{
		super(newText);
		parentWidget = newParent;
	}
	
	/**
	 * Creates a button with specified text, parent, and parameters for the layout manager
	 * 
	 * @param newParent The parent container
	 * @param newText The text to be displayed on the face of the widget
	 * @param parameters The layout parameters
	 */
	public SpaglButton(SpaglCoreContainer newParent, String newText, String parameters)
	{
		super(newText);
		parentWidget = newParent;
		parentWidget.placeWidget(this, parameters);
	}

	/**
	 * Adds an event listener to this button
	 * 
	 * @param eventID The ID of the event to be listened for
	 * @param method The (string) name of the method to be called when the event is fired
	 * @param targetObject The object where the method should be fired 
	 */
	public void addEventListener(int eventID, String method, Object targetObject) {
		try{
			switch(eventID)
			{
				case SpaglEvent.SELECTION_EVENT: {this.addActionListener(new SpaglEventListener(targetObject.getClass().getMethod(method, SpaglCoreEvent.class), targetObject, SpaglEvent.SELECTION_EVENT)); break;}
				default: throw new IllegalArgumentException("Event not supported by this widget");
			}}
		catch (SecurityException e) {e.printStackTrace();} 
		catch (NoSuchMethodException e) {e.printStackTrace();}
		
	}

	/**
	 * Gets the parent container for the object
	 * 
	 * @return The {@link SpaglContainer} for this object as a {@link SpaglCoreContainer}
	 */
	public SpaglCoreContainer getContainer() {
		return parentWidget;
	}

	/**
	 * Gets the widget's font
	 * 
	 * @return The {@link SpaglFont} associated with the object as a {@link SpaglCoreFont}
	 */
	public SpaglCoreFont getWidgetFont() {
		return widgetFont;
	}

	/**
	 * Sets the font for this widget
	 * 
	 * @param newFont a {@link SpaglFont} to associate with the object
	 */
	public void setFont(SpaglCoreFont newFont) {
		widgetFont = newFont;
		super.setFont((SpaglFont)newFont);
	}
	
	
	/**
	 * Simulates disposal of the font resource for cross library purposes
	 * 
	 * @param newFont A {@link SpaglFont} that will be set as the font for this widget
	 * @param dispose In a cross library sense, would dispose instantly if set to true
	 */
	public void setFont(SpaglCoreFont newFont, boolean dispose) {
		if(!dispose)
			widgetFont = newFont;
		else
			widgetFont = null;
		super.setFont((SpaglFont)newFont);
	}
	
	/**
	 * Gets the image associated with this widget
	 * 
	 * @return The {@link SpaglImage} associated with this widget
	 */
	public SpaglCoreImage getImage() {
		return widgetImage;
	}

	/**
	 * Sets the image for this widget
	 * 
	 * @param newImage The {@link SpaglImage} that is associated with the widget
	 */
	public void setImage(SpaglCoreImage newImage) {
		widgetImage = newImage;
		super.setIcon((SpaglImage)widgetImage);
	}
	
	/**
	 * Simulates disposal of the image resource for cross library purposes
	 * 
	 * @param newImage A {@link SpaglImage} that will be set as the image for this widget
	 * @param dispose In a cross library sense, would dispose instantly if set to true
	 */
	public void setImage(SpaglCoreImage newImage, boolean dispose) {
		if(!dispose)
			widgetImage = newImage;
		else
			widgetImage = null;
		super.setIcon((SpaglImage)newImage);
	}
	
	/**
	 * Swing does not need manual disposal, so this is here just for cross library purposes
	 */ 
	public void dispose() {}
	
}
